package com.aopcinemas.cinema.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.aopcinemas.cinema.model.Invoice;
import com.aopcinemas.cinema.model.Schedule;
import com.aopcinemas.cinema.model.Session;

public class SessionDao {

    private Connection connection;

    public SessionDao(Connection connection) {
        this.connection = connection;
    }

    public void addSession(Session session) {
        try {
            Statement st = connection.createStatement();
            String sql;
            if (session.getInvoice() == null) {
                sql = "insert into session(row, seat, schedule_id) values(" + session.getRow() + ", " + session.getSeat() + ", " + session.getSchedule().getId() + " )";
            } else {
                sql = "insert into session(row, seat, schedule_id, invoice_id) values(" + session.getRow() + ", " + session.getSeat() + ", " + session.getSchedule().getId() + ", " + session.getInvoice().getId() + ")";
            }

            int rows = st.executeUpdate(sql);
            if (rows <= 0) {
                throw new RuntimeException("Can't add session to database");
            }
            st.close();
        } catch (SQLException e) {
            throw new RuntimeException("System error", e);
        }
    }

    public List<Session> getSessions(Schedule schedule) {
        return getSessions(schedule, false);
    }

    public List<Session> getSessions(Schedule schedule, boolean filterOrdered) {
        List<Session> sessions = new ArrayList<Session>();

        try {
            String sql = "select * from session ";
            if (schedule != null) {
                sql = sql + " where schedule_id = " + schedule.getId();
                if (filterOrdered) {
                    sql = sql + " and invoice_id is null";
                }
            }

            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                Session session = new Session();
                session.setId(rs.getLong("id"));
                session.setRow(rs.getInt("row"));
                session.setSeat(rs.getInt("seat"));
                if (schedule != null) {
                    session.setSchedule(schedule);
                } else {
                    Schedule s = new Schedule();
                    s.setId(rs.getLong("schedule_id"));
                    session.setSchedule(schedule);
                }
                Object invoiceIdObject = rs.getObject("invoice_id");
                if (invoiceIdObject != null) {
                    Invoice invoice = new Invoice();
                    invoice.setId((Long) invoiceIdObject);
                    session.setInvoice(invoice);
                }
                sessions.add(session);
            }
            rs.close();
            st.close();
        } catch (SQLException e) {
            throw new RuntimeException("System error", e);
        }

        return sessions;
    }
}
